//
//  MineViewController.m
//  FengyuanXS
//
//  Created by 王冬 on 2020/7/25.
//  Copyright © 2020 王冬. All rights reserved.
//

#import "MineViewController.h"
#import "LoginViewController.h"
#import "RootViewController.h"
#import "UserViewController.h"
#import "SecurityCenterController.h"
#import "AboutViewController.h"
#import "FyxsViewController.h"
#import "MyAppealListController.h"
@interface MineViewController ()
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic ,strong)UserModel *model;
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *companyNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *userName;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;


@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    [self getUserData];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section ==0) {
        return 2;
    }
    if (section ==1) {
        return 1;
    }
    if (section == 2) {
        return 3;
    }
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 10)];
    
    view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    return  view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return  10;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            UserViewController *userVC = [UserViewController loadStroyboard:[UserViewController new]];
            userVC.model = self.model;
            userVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:userVC animated:YES];
        }
        if (indexPath.row ==1) {
            
            MyAppealListController *appealVC = [MyAppealListController loadStroyboard:[MyAppealListController new]];
            appealVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:appealVC animated:YES];
        }
        
    }
    if (indexPath.section ==1) {
        if (indexPath.row ==0) {
            SecurityCenterController *secVC = [SecurityCenterController loadStroyboard:[SecurityCenterController new]];
            secVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:secVC animated:YES];
            
        }
    }
    if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            FyxsViewController *aboutVC = [FyxsViewController loadStroyboard:[FyxsViewController new]];
            aboutVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:aboutVC animated:YES];
            
        }
        
        if (indexPath.row == 1) {
            WebViewController *aboutVC = [[WebViewController alloc]init];
            aboutVC.noticeID = @"61";
            aboutVC.titleStr = @"平台使用说明";
            aboutVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:aboutVC animated:YES];
            
        }
        if (indexPath.row == 2) {
            AboutViewController *aboutVC = [AboutViewController loadStroyboard:[AboutViewController new]];
            aboutVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:aboutVC animated:YES];
            
        }
        
        
        
    }
    
    
}
- (IBAction)quitBtn:(UIButton *)sender {
    
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"您确定要退出当前账户吗?" preferredStyle:UIAlertControllerStyleAlert];
    
    // Create the actions.
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
     
    }];
    
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
           [TokenManager removeUserId];
           [TokenManager removeToken];
           [self quitApp];
       
    }];
    
    // Add the actions.
    [alertController addAction:cancelAction];
    [alertController addAction:otherAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
   
    
}


-(void)quitApp{
    
    

    LoginViewController *loginVC = [LoginViewController loadStroyboard:[LoginViewController new]];
    
    UINavigationController * nav = [[RootViewController alloc] initWithRootViewController:loginVC];
    nav.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:nav animated:YES completion:nil];
    
}
- (UIViewController *)visibleViewController {
    UIViewController *rootViewController = self.window.rootViewController;
    return [self getVisibleViewControllerFrom:rootViewController];
}

-(UIViewController *) getVisibleViewControllerFrom:(UIViewController *) vc {
    
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self getVisibleViewControllerFrom:[((UINavigationController *) vc) visibleViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self getVisibleViewControllerFrom:[((UITabBarController *) vc) selectedViewController]];
    } else {
        if (vc.presentedViewController) {
            return [self getVisibleViewControllerFrom:vc.presentedViewController];
        } else {
            return vc;
        }
    }
}

-(void)getUserData{
    WS(weakSelf);
    [[ToastManager shardInstance]showSystemProgress:self.view];
    [[ApiManager ShardInstance]userinfoResultBlock:^(NSDictionary *data, NSError *error) {
        UserModel *model = [UserModel mj_objectWithKeyValues:data[@"rs"]];
        self.model = model;
        [[ToastManager shardInstance]hideProgress:self.view];
        
        weakSelf.companyNameLabel.text = model.company_name;
        weakSelf.userName.text = model.real_name;
        weakSelf.phoneLabel.text = [TokenManager getToken];
        
    }];
}



/*
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
 
 // Configure the cell...
 
 return cell;
 }
 */

/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
